﻿using System;
using System.Globalization;

namespace OpenWaves.EPiServer.Localization
{
    public abstract class TranslationEntry : ITranslationEntry, IEquatable<TranslationEntry>
    {
        private readonly string name;

        private readonly TranslationKeyCollectionBase parent;

        protected TranslationEntry(string name, TranslationKeyCollectionBase parent)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Name cannot be empty", "name");
            }

            this.parent = parent;
            this.name = name;
        }

        public string Name
        {
            get { return this.name; }
        }

        public virtual string Path
        {
            get { return this.parent == null ? "/" + this.name : string.Format("{0}/{1}", this.parent.Path, this.name); }
        }

        public string Text
        {
            get { return this.GetString(); }
        }

        public static bool operator ==(TranslationEntry left, TranslationEntry right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(TranslationEntry left, TranslationEntry right)
        {
            return !Equals(left, right);
        }

        public bool Equals(TranslationEntry other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }
            if (ReferenceEquals(this, other))
            {
                return true;
            }
            return Equals(other.parent, this.parent) && Equals(other.name, this.name);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }
            if (ReferenceEquals(this, obj))
            {
                return true;
            }
            if (obj.GetType() != typeof(TranslationEntry))
            {
                return false;
            }
            return this.Equals((TranslationEntry)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((this.parent != null ? this.parent.GetHashCode() : 0) * 397) ^ (this.name != null ? this.name.GetHashCode() : 0);
            }
        }

        public override string ToString()
        {
            return this.Path;
        }

        public virtual string GetString()
        {
            return this.GetStringByCulture(CultureInfo.CurrentUICulture);
        }

        public abstract string GetStringByCulture(CultureInfo culture);
    }
}